//package com.example.demo.ds;
//
//import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
//import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
//import com.example.demo.sm.SmEncryptionUtil;
//import com.zaxxer.hikari.HikariConfig;
//import com.zaxxer.hikari.HikariDataSource;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//
//import javax.sql.DataSource;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author Administrator
// */
//@Configuration
//public class DecryptingYmlDynamicDataSourceProvider extends AbstractDataSourceProvider {
//
//    private final Environment env;
//
//    public DecryptingYmlDynamicDataSourceProvider(Environment env) {
//        this.env = env;
//    }
//
//    @Override
//    public Map<String, DataSource> loadDataSources() {
//        Map<String, DataSourceProperty> propertiesMap = bindDataSourceProperties(env);
//        Map<String, DataSource> dataSourceMap = new HashMap<>(propertiesMap.size());
//        for (Map.Entry<String, DataSourceProperty> item : propertiesMap.entrySet()) {
//            DataSourceProperty property = item.getValue();
//            // 解密用户名和密码
//            property.setUrl(property.getUrl());
//            property.setUsername(property.getUsername());
//            property.setPassword(property.getPassword());
//            dataSourceMap.put(item.getKey(), createDataSource(property));
//        }
//        return dataSourceMap;
//    }
//
//    private String decrypt(String encrypted) {
//        // 这里是你的解密逻辑，返回解密后的字符串
//        return SmEncryptionUtil.sm2Decrypt(encrypted);
//    }
//
//    private Map<String, DataSourceProperty> bindDataSourceProperties(Environment env) {
//        String dataSource1Url = decrypt(env.getProperty("spring.datasource.dynamic.datasource.master.url"));
//        String dataSource1Username = decrypt(env.getProperty("spring.datasource.dynamic.datasource.master.username"));
//        String dataSource1Password = decrypt(env.getProperty("spring.datasource.dynamic.datasource.master.password"));
//        String dataSource1DriverClassName = env.getProperty("spring.datasource.dynamic.datasource.master.driver-class-name");
//
//        DataSourceProperty dataSourceProperty1 = new DataSourceProperty();
//        dataSourceProperty1.setUrl(dataSource1Url);
//        dataSourceProperty1.setUsername(dataSource1Username);
//        dataSourceProperty1.setPassword(dataSource1Password);
//        dataSourceProperty1.setDriverClassName(dataSource1DriverClassName);
//
//        Map<String, DataSourceProperty> propertiesMap = new HashMap<>(2);
//        propertiesMap.put("dataSource1", dataSourceProperty1);
//        return propertiesMap;
//    }
//
//    private DataSource createDataSource(DataSourceProperty property) {
//        HikariConfig config = new HikariConfig();
//        config.setJdbcUrl(property.getUrl());
//        config.setUsername(property.getUsername());
//        config.setPassword(property.getPassword());
//        config.setDriverClassName(property.getDriverClassName());
//        return new HikariDataSource(config);
//    }
//
//
//}
